<?php
class ControllerManagementUser extends Controller {
	private $error = array();
	
	public function index() {
		$this->document->setTitle('用户管理');
		
		$this->children = array(
				'common/header',
				'common/footer'
				);
		$this->load->model('management/user');
		$this->getList();
	}
	
	private function getFilterFormUrl() {
		//这种拼URL的方式比较原始，跑通后考虑发成http_build_query()+Array（）的方式构建
		$url = 'token=' . $this->session->data['token'];
		
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
		
		if(isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'],ENT_QUOTES, 'UTF-8'));
		}
		
		if(isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}
		
		if(isset($this->request->get['filter_date_added'])) {
			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
		}
		
		return $url;
	}
	
	private function getSortUrl($order, $sort){
		
		$url = $this->getFilterFormUrl();
		
		if($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
		
		if(!empty($sort)) {
			$url .= '&sort=' . $sort;	
		}
		
		if( isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		return $url;
	}
	
	private function getFilterUrl() {
		
		$url = $this->getFilterFormUrl();
		
		if(isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		
		if(isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if( isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		return $url;
	}
	
	private function getPaginationUrl() {
		$url = $this->getFilterFormUrl();
		$url .= '&page={page}';
		return $url;
	}
	
	private function getList() {
		if(isset($this->request->get['filter_fullname'])) {
			$filter_fullname = $this->request->get['filter_fullname'];
		} else {
			$filter_fullname = null;
		}
		
		if(isset($this->request->get['filter_email'])) {
			$filter_email = $this->request->get['filter_email'];
		} else {
			$filter_email = null;
		}
		
		if(isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}
		
		if(isset($this->request->get['filter_date_added'])) {
			$filter_date_added = $this->request->get['filter_date_added'];
		} else {
			$filter_date_added = null;
		}
		
		if(isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'fullname';
		}
		
		if(isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if(isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$this->data['breadcrumbs'] = array();
		
		$this->data['breadcrumbs'][] = array(
				'text'      => '首页',
				'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
				'separator' => false
		);
		
		$this->data['breadcrumbs'][] = array(
				'text'      => '注册用户管理',
				'href'      => $this->url->link('management/user', $this->getFilterUrl(), 'SSL'),
				'separator' => ' :: '
		);
		
		$this->data['insert'] = $this->url->link('management/user/insert',$this->getFilterUrl(), 'SSL');
		$this->data['delete'] = $this->url->link('management/user/delete',$this->getFilterUrl(), 'SSL');
		
		$this->data['users'] = array();
		
		$data = array(
				'filter_fullname'	=> $filter_fullname,
				'filter_email'		=> $filter_email,
				'filter_status'		=> $filter_status,
				'filter_date_added'	=> $filter_date_added,
				'sort'				=> $sort,
				'order'				=> $order,
				'start'				=> ($page-1) * 10,
				'limit'				=> 10
				);
		
		$user_total = $this->model_management_user->getTotalUsers($data);
		$results = $this->model_management_user->getUsers($data);
		
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' 	=> '更新',
				'href'	=> $this->url->link('management/user/update', $this->getFilterUrl(), 'SSL')
			);
			
			$this->data['users'][] = array(
				'user_id'		=> $result['user_id'],
				'fullname'		=> $result['fullname'],
				'email'			=> $result['email'],
				'gender'		=> $result['gender'],
				'title'			=> $result['title'],
				'name'			=> $result['name'],
				'company'		=> $result['company'],
				'country_id'	=> $result['country_id'],
				'zone'			=> $result['zone'],
				'address_1'		=> $result['address_1'],
				'address_2'		=> $result['address_2'],
				'city'			=> $result['city'],
				'postcode'		=> $result['postcode'],
				'website'		=> $result['website'],
				'telephone'		=> $result['telephone'],
				'fax'			=> $result['fax'],
				'ip'			=> $result['ip'],
				'status'		=> $result['status'],
				'date_added'	=> date('d/m/Y', strtotime($result['date_added'])),
				'selected'		=> isset($this->request->post['selected']) && in_array($result['user_id'], $this->request->post['selected']),
				'action'		=> $action
			);
		}
		
		$this->data['token'] = $this->session->data['token'];
		
		if(isset($this->error['warning'])) {
			$this->data['error_warning'] = '警告：有不合法的因素';
		} else {
			$this->data['error_warning'] = '';
		}
		
		if(isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$this->data['sort_fullname'] = $this->url->link('management/user',$this->getSortUrl($order,'fullname'), 'SSL');
		$this->data['sort_email'] = $this->url->link('management/user',$this->getSortUrl($order,'u.email'),'SSL');
		$this->data['sort_status'] = $this->url->link('management/user',$this->getSortUrl($order,'u.status'),'SSL');
		$this->data['sort_date_added'] = $this->url->link('management/user',$this->getSortUrl($order,'u.date_added'),'SSL');
		
		$pagination = new Pagination();
		$pagination->total = $user_total;
		$pagination->page = $page;
		$pagination->limit = 15;
		$pagination->text = "显示{start} - {end} 之 {total} (共计{pages} 页)";
		$pagination->url = $this->url->link('management/user',$this->getPaginationUrl(),'SSL');
		
		$this->data['pagination'] = $pagination->render();
		
		$this->data['filter_fullname'] = $filter_fullname;
		$this->data['filter_email'] = $filter_email;
		$this->data['filter_status'] = $filter_status;
		$this->data['filter_date_added'] = $filter_date_added;
		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		
		$this->template = 'management/user_list.tpl';
		$this->children = array(
				'common/header',
				'common/footer'
		);
		
		$this->response->setOutput($this->render());
	}
	
	
}